﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using MyClub.Model;

namespace MyClub.Web.Routing
{
    /// <summary>
    /// Defines routing logic for <see cref="Post"/> elements.
    /// </summary>
    public class PostRoute: RouteBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PostRoute"/> class.
        /// </summary>
        /// <param name="virtualPath">The virtual path.</param>
        public PostRoute(string virtualPath)
        {
            if (string.IsNullOrEmpty(virtualPath))
                throw new ArgumentException(Properties.Resources.VirtualPathIsNullOrEmpty, "virtualPath");

            defaultRouteHandler = new VirtualPathRouteHandler(virtualPath);
        }

        private VirtualPathRouteHandler defaultRouteHandler;

        /// <summary>
        /// When overridden in a derived class, returns route information about the request.
        /// </summary>
        /// <param name="httpContext">An object that encapsulates information about the HTTP request.</param>
        /// <returns>
        /// An object that contains the values from the route definition if the route matches the current request, or null if the route does not match the request.
        /// </returns>
        public override RouteData GetRouteData(System.Web.HttpContextBase httpContext)
        {
            if (httpContext == null)
                throw new ArgumentNullException("httpContext");

            using (var entities = new MyClubEntities())
            {
                string path = httpContext.Request.Path;

                var postQuery = from p in entities.Post.Include("Category")
                                    where string.Compare(p.Path, path, StringComparison.OrdinalIgnoreCase) == 0
                                    select p;

                var post = postQuery.FirstOrDefault();
                if (post == null)
                    return null;

                var routeData = new RouteData(this, defaultRouteHandler);
                routeData.Values["PostId"] = post.Id;
                routeData.Values["PostPath"] = post.Path;
                if (post.Category != null)
                    routeData.Values["CategoryId"] = post.Category.Id;
                routeData.Values["CategoryPath"] = post.Category.Path;
                return routeData;
            }
        }

        /// <summary>
        /// When overridden in a derived class, checks whether the route matches the specified values, and if so, generates a URL and retrieves information about the route.
        /// </summary>
        /// <param name="requestContext">An object that encapsulates information about the requested route.</param>
        /// <param name="values">An object that contains the parameters for a route.</param>
        /// <returns>
        /// An object that contains the generated URL and information about the route, or null if the route does not match <paramref name="values"/>.
        /// </returns>
        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            if (requestContext == null)
                throw new ArgumentNullException("requestContext");

            if (values == null)
                throw new ArgumentNullException("values");

            if (!values.ContainsKey("PostId"))
                return null;

            // TODO: Allow for virtualPath override from the entity model
            return new VirtualPathData(this, defaultRouteHandler.VirtualPath);
        }
    }
}
